/**
 * Copyright � 2012 GestureSoft. ALL RIGHTS RESERVED.
 * This file contains proprietary and GestureSoft CONFIDENTIAL Information.
 * Use, disclosure or reproduction is prohibited.
 * 
 * @fileName StyleParser.as
 * @creationDate Aug 8, 2012
 * @author 
 * @version 1.0
 */
package com.gesturesoft.imultimax.model
{
	
	import com.gesturesoft.imultimax.events.CompleteEvent;
	import com.gesturesoft.imultimax.model.interfaces.IStyleParser;
	import com.gesturesoft.imultimax.model.loader.CustomLoader;
	import com.gesturesoft.imultimax.model.loader.events.LoadFaultEvent;
	import com.gesturesoft.imultimax.model.loader.events.LoadResultEvent;
	import com.gesturesoft.imultimax.model.loader.interfaces.ILoader;
	import com.gesturesoft.imultimax.model.vo.AppVO;
	import com.gesturesoft.imultimax.model.vo.AssetVO;
	import com.gesturesoft.imultimax.model.vo.StyleMainVO;
	import com.gesturesoft.imultimax.model.vo.StyleVO;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	import flash.xml.XMLDocument;
	import flash.xml.XMLNode;
	
	import mx.collections.ArrayCollection;

	public class StyleParser extends EventDispatcher implements IStyleParser
	{
		
		private var _assetDetails:ArrayCollection;
		private var _assetCount:int = 0;
		private var _styles:ArrayCollection;
		
		public function StyleParser(  val : ArrayCollection )
		{
			
			assetDetails = val;
			_assetCount = assetDetails.length;
			_styles = new ArrayCollection();
			
		}
		
		/**
		 * Parse the style xml in the extracted asset folder
		 */ 
		
		public function parse():void
		{
			
			_assetCount -= 1;
			if(_assetCount >= 0)
			{
				
				var appVO:AppVO = assetDetails[_assetCount];
				
				var url:String = File.applicationStorageDirectory.resolvePath(appVO.name+"/assets/data/styles.xml").url;
				var loader:ILoader = new CustomLoader(url);
				
				EventDispatcher( loader ).addEventListener( LoadResultEvent.LOAD_COMPLETE, onLoadComplete );
				EventDispatcher( loader ).addEventListener( LoadFaultEvent.LOAD_FAILED, onLoadFailed );
				
				loader.load();
				
			}
			else
			{
				
				styles = _styles;
				dispatchEvent(new CompleteEvent(CompleteEvent.COMPLETE, this));
				return;
				
			}
			
		}
		
		private function onLoadComplete(event : LoadResultEvent):void
		{
			
			var styleMain : StyleMainVO = new StyleMainVO();
			
			styleMain.company = XML(event.data).company.toString().replace(" ", "");
			
			//START:Extract asset details and add to style main vo 
			var assetXML:XML = XML(event.data).assets[0];
			styleMain.assetVersion = assetXML.attribute("ver").toString();
			styleMain.assets = new ArrayCollection();
			
			for each(var item:XML in assetXML.*)
			{
				
				var assetVO:AssetVO = new AssetVO();
				
				assetVO.name = item.localName();
				assetVO.type = item.attribute("type").toString();
				assetVO.value = item.toString().replace(" ", "");
				assetVO.activeTablePropery =  item.attribute("activeTablePropery").toString();
				
				styleMain.assets.addItem(assetVO);
			}
			
			//END:Extract asset details and add to style main vo 
			
			//START:Extract style details and add to style main vo
			var stylesXML:XML = XML(event.data).styles[0];
			styleMain.styleVersion = stylesXML.attribute("ver").toString();
			styleMain.styles = new ArrayCollection();
			
			for each(var style:XML in stylesXML.*)
			{
				
				var styleVO:StyleVO = new StyleVO();
				
				styleVO.className = style.className.toString().replace(" ", "");
				styleVO.property = style.property.toString().replace(" ", "");
				styleVO.value = style.value.toString().replace(" ", "");
				styleVO.activeTablePropery = style.activeTablePropery.toString().replace(" ", "");
				styleVO.activeTableSubPropery = style.atSubProperty.toString().replace(" ", "");
				
				trace(style.activeTablePropery.toString());
				trace(style.className.toString());
				trace(style.property.toString());
				trace(style.value.toString());
				
				styleMain.styles.addItem(styleVO);
				
			}
			//END:Extract style details and add to style main vo 
			
			_styles.addItem(styleMain);
			
			parse();
			
		}
		
		private function onLoadFailed(event : LoadFaultEvent):void
		{
			
		}
		
		public function get assetDetails():ArrayCollection
		{
			
			return _assetDetails;
			
		}
		
		public function set assetDetails(val : ArrayCollection):void
		{
			
			_assetDetails = val;
			
		}
		
		public function get styles():ArrayCollection
		{
			
			return _styles;
			
		}
		
		public function set styles(val : ArrayCollection):void
		{
			
			_styles = val;
			
		}
		
	}
}